home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / BaseSLList.cc < prev    next >
C/C++ Source or Header  |  1997-01-08  |  5KB  |  259 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988, 1992 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19. #if defined (__GNUG__)
  20. #pragma implementation
  21. #endif
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <limits.h>
  28. #include <stream.h>
  29. #include <builtin.h>
  30. #include "BaseSLList.h"
  31.  
  32. #include "error.h"
  33.  
  34. void BaseSLList::error(const char* msg) const
  35. {
  36.   ::error ("SLList: %s", msg);
  37. }
  38.  
  39. int BaseSLList::length() const
  40. {
  41.   int l = 0;
  42.   BaseSLNode* t = last;
  43.   if (t != 0) do { ++l; t = t->tl; } while (t != last);
  44.   return l;
  45. }
  46.  
  47. void BaseSLList::clear()
  48. {
  49.   if (last == 0)
  50.     return;
  51.  
  52.   BaseSLNode* p = last->tl;
  53.   last->tl = 0;
  54.   last = 0;
  55.  
  56.   while (p != 0)
  57.   {
  58.     BaseSLNode* nxt = p->tl;
  59.     delete_node(p);
  60.     p = nxt;
  61.   }
  62. }
  63.  
  64.  
  65. // Note:  This is an internal method.  It does *not* free old contents!
  66.  
  67. void BaseSLList::copy(const BaseSLList& a)
  68. {
  69.   if (a.last == 0)
  70.     last = 0;
  71.   else
  72.   {
  73.     BaseSLNode* p = a.last->tl;
  74.     BaseSLNode* h = copy_node(p->item());
  75.     last = h;
  76.     for (;;)
  77.     {
  78.       if (p == a.last)
  79.       {
  80.         last->tl = h;
  81.         return;
  82.       }
  83.       p = p->tl;
  84.       BaseSLNode* n = copy_node(p->item());
  85.       last->tl = n;
  86.       last = n;
  87.     }
  88.   }
  89. }
  90.  
  91. BaseSLList& BaseSLList::operator = (const BaseSLList& a)
  92. {
  93.   if (last != a.last)
  94.   {
  95.     clear();
  96.     copy(a);
  97.   }
  98.   return *this;
  99. }
  100.  
  101. Pix BaseSLList::prepend(const void *datum)
  102. {
  103.   return prepend(copy_node(datum));
  104. }
  105.  
  106.  
  107. Pix BaseSLList::prepend(BaseSLNode* t)
  108. {
  109.   if (t == 0) return 0;
  110.   if (last == 0)
  111.     t->tl = last = t;
  112.   else
  113.   {
  114.     t->tl = last->tl;
  115.     last->tl = t;
  116.   }
  117.   return Pix(t);
  118. }
  119.  
  120.  
  121. Pix BaseSLList::append(const void *datum)
  122. {
  123.   return append(copy_node(datum));
  124. }
  125.  
  126. Pix BaseSLList::append(BaseSLNode* t)
  127. {
  128.   if (t == 0) return 0;
  129.   if (last == 0)
  130.     t->tl = last = t;
  131.   else
  132.   {
  133.     t->tl = last->tl;
  134.     last->tl = t;
  135.     last = t;
  136.   }
  137.   return Pix(t);
  138. }
  139.  
  140. void BaseSLList::join(BaseSLList& b)
  141. {
  142.   BaseSLNode* t = b.last;
  143.   b.last = 0;
  144.   if (last == 0)
  145.     last = t;
  146.   else if (t != 0)
  147.   {
  148.     BaseSLNode* f = last->tl;
  149.     last->tl = t->tl;
  150.     t->tl = f;
  151.     last = t;
  152.   }
  153. }
  154.  
  155. Pix BaseSLList::ins_after(Pix p, const void *datum)
  156. {
  157.   BaseSLNode* u = (BaseSLNode*)p;
  158.   BaseSLNode* t = copy_node(datum);
  159.   if (last == 0)
  160.     t->tl = last = t;
  161.   else if (u == 0) // ins_after 0 means prepend
  162.   {
  163.     t->tl = last->tl;
  164.     last->tl = t;
  165.   }
  166.   else
  167.   {
  168.     t->tl = u->tl;
  169.     u->tl = t;
  170.     if (u == last) 
  171.       last = t;
  172.   }
  173.   return Pix(t);
  174. }
  175.  
  176. void BaseSLList::del_after(Pix p)
  177. {
  178.   BaseSLNode* u = (BaseSLNode*)p;
  179.   if (last == 0 || u == last) error("cannot del_after last");
  180.   if (u == 0) u = last; // del_after 0 means delete first
  181.   BaseSLNode* t = u->tl;
  182.   if (u == t)
  183.     last = 0;
  184.   else
  185.   {
  186.     u->tl = t->tl;
  187.     if (last == t)
  188.       last = u;
  189.   }
  190.   delete_node(t);
  191. }
  192.  
  193. int BaseSLList::owns(Pix p) const
  194. {
  195.   BaseSLNode* t = last;
  196.   if (t != 0 && p != 0)
  197.   {
  198.     do
  199.     {
  200.       if (Pix(t) == p) return 1;
  201.       t = t->tl;
  202.     } while (t != last);
  203.   }
  204.   return 0;
  205. }
  206.  
  207. int BaseSLList::remove_front(void *dst, int signal_error)
  208. {
  209.   if (last)
  210.   {
  211.     BaseSLNode* t = last->tl;
  212.     copy_item(dst, t->item());
  213.     if (t == last)
  214.       last = 0;
  215.     else
  216.       last->tl = t->tl;
  217.     delete_node(t);
  218.     return 1;
  219.   }
  220.   if (signal_error)
  221.     error("remove_front of empty list");
  222.   return 0;
  223. }
  224.  
  225. void BaseSLList::del_front()
  226. {
  227.   if (last == 0) error("del_front of empty list");
  228.   BaseSLNode* t = last->tl;
  229.   if (t == last)
  230.     last = 0;
  231.   else
  232.     last->tl = t->tl;
  233.   delete_node(t);
  234. }
  235.  
  236. int BaseSLList::OK() const
  237. {
  238.   int v = 1;
  239.   if (last != 0)
  240.   {
  241.     BaseSLNode* t = last;
  242.     long count = LONG_MAX;      // Lots of chances to find last!
  243.     do
  244.     {
  245.       count--;
  246.       t = t->tl;
  247.     } while (count > 0 && t != last);
  248.     v &= count > 0;
  249.   }
  250.   if (!v) error("invariant failure");
  251.   return v;
  252. }
  253.  
  254. /*
  255. ;;; Local Variables: ***
  256. ;;; mode: C++ ***
  257. ;;; End: ***
  258. */
  259.